package com.yanqu.road.server.cmd.activity.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionwar.enums.eTimeInfoPeriodType;
import com.yanqu.road.entity.activity.unionwar.result.CurrentTimeInfo;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.unionwar.UnionPhaseWarMgr;
import com.yanqu.road.server.manger.activity.unionwar.UnionWar;
import com.yanqu.road.server.manger.activity.unionwar.UnionWarActivityMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = GameProtocol.S_UNION_WAR_ACTIVITY_FINALS_RESULT_OR_MATCH_REQ, desc = "决赛匹配或结果数据")
public class CrossCrossFinalsResultOrMatchInfoReqCmd extends AbstractCommand {

    private int activityId;
    private int session;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.CrossFinalsResultOrMatchInfoReqMsg reqMsg = UnionWarActivityServerProto.CrossFinalsResultOrMatchInfoReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        session = reqMsg.getSession();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = UnionWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        UnionWar unionWar = UnionPhaseWarMgr.getUnionWarMap().get(activityId);
        if(unionWar == null){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        CurrentTimeInfo currentTimeInfo = unionWar.getCurrentTimeInfo();
        if(currentTimeInfo.getTimeInfo() == null && currentTimeInfo.getPeriodType() != eTimeInfoPeriodType.ReceiveTime){
            ret = GameErrorCode.E_UNION_WAR_ACTIVITY_IS_NOT_IN_FINALS_PHASE;
            return;
        }
        if(currentTimeInfo.getTimeInfo() != null && currentTimeInfo.getTimeInfo().getType() == 1){
            ret = GameErrorCode.E_UNION_WAR_ACTIVITY_IS_NOT_IN_FINALS_PHASE;
            return;
        }
        //上跨服
        UnionWarActivityServerProto.CrossFinalsResultOrMatchInfoReqMsg.Builder reqMsg = UnionWarActivityServerProto.CrossFinalsResultOrMatchInfoReqMsg.newBuilder();
        reqMsg.setActivityId(activityId);
        reqMsg.setSession(session);
        int groupId = GameUnionActivityGroupMgr.getViewDataGroupId(activityId, packet.getPlayerId());
        player.sendUnionActivityGroupPacket(Protocol.C_CROSS_UNION_WAR_FINALS_RESULT_OR_MATCH_REQ, reqMsg, activityId, groupId);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionWarActivityServerProto.CrossFinalsResultOrMatchInfoRespMsg.Builder respMsg = UnionWarActivityServerProto.CrossFinalsResultOrMatchInfoRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setActivityId(activityId);
            respMsg.setSession(session);
            player.sendPacket(Protocol.U_UNION_WAR_ACTIVITY_FINALS_RESULT_OR_MATCH_REQ, respMsg);
        }
    }
}
